package com.lq.employmentmanage.config;

import com.alibaba.fastjson.JSONObject;
import java.lang.reflect.Method;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

@Aspect
@Component
@Slf4j
public class LogFilter {

  @Pointcut("execution(public * com.lq.employmentmanage.controller..*.*(..))")
  public void Pointcut() {
  }

  @Before("Pointcut()")
  public void before(JoinPoint joinPoint) {
    Object args[] = joinPoint.getArgs();
    MethodSignature sig = (MethodSignature) joinPoint.getSignature();
    Method method = sig.getMethod();
    if (null != method.getDeclaringClass().getName() && null != method.getName() && null != args
        && args.length > 0) {
      log.info("{} . {} : 请求参数：{}", method.getDeclaringClass().getName(), method.getName(),
          StringUtils.join(args, " : "));
    }
  }

  @AfterReturning(value = "Pointcut()", returning = "rvt")
  public void after(JoinPoint joinPoint, Object rvt) {
    MethodSignature sig1 = (MethodSignature) joinPoint.getSignature();
    Method method1 = sig1.getMethod();
    if (null != rvt && null != method1.getDeclaringClass()) {
      try {
        log.info("{} . {} : 返回数据：{}", method1.getDeclaringClass().getName(), method1.getName(),
            JSONObject.toJSONString(rvt));
      } catch (Exception e) {
      }
    }
  }

}
